package net.learn.piggy.listener;

import net.learn.piggy.domain.MyBootBean;
import net.learn.piggy.bean.User;
import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.boot.SpringApplication;

public class MySpringApplicationRunListener implements SpringApplicationRunListener {
    public MySpringApplicationRunListener(SpringApplication application, String[] args){
        application.addListeners(new MyApplicationListener());
        System.out.println("MySpringApplicationRunListener --> MySpringApplicationRunListener");
    }

    @Override
    public void starting(ConfigurableBootstrapContext bootstrapContext) {
        MyBootBean registry = bootstrapContext.get(MyBootBean.class);
        System.out.println("MySpringApplicationRunListener --> starting --> "+registry.toString());
    }

    @Override
    public void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment) {
        MyBootBean registry = bootstrapContext.get(MyBootBean.class);
        System.out.println("MySpringApplicationRunListener --> environmentPrepared --> "+registry.toString());
    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {
        System.out.println("MySpringApplicationRunListener --> contextPrepared");
    }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context) {
        System.out.println("MySpringApplicationRunListener --> contextLoaded");
    }

    @Override
    public void started(ConfigurableApplicationContext context) {
        User user = context.getBean(User.class);
        System.out.println("MySpringApplicationRunListener --> started --> "+user);
    }

    @Override
    public void running(ConfigurableApplicationContext context) {
        User user = context.getBean(User.class);
        System.out.println("MySpringApplicationRunListener --> running --> "+user);
    }

    @Override
    public void failed(ConfigurableApplicationContext context, Throwable exception) {
        System.out.println("MySpringApplicationRunListener --> failed");
    }
}
